package com.android2024.no7_paino;

import androidx.appcompat.app.AppCompatActivity;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    private SoundPool soundPool;
    private HashMap<Integer,Integer> map = new HashMap<>();
    private int[] viewIds={R.id.id_do,R.id.id_re,R.id.id_mi,R.id.id_fa,R.id.id_so,
            R.id.id_la,R.id.id_si};
    private int[] rawIds = {R.raw.music_do,R.raw.music_re,R.raw.music_mi,R.raw.music_fa,
            R.raw.music_so,R.raw.music_la,R.raw.music_si};
    private void init(){
        soundPool = new SoundPool(rawIds.length, AudioManager.STREAM_MUSIC,0);
        for(int i=0;i<viewIds.length;i++){
            findViewById(viewIds[i]).setOnClickListener(this);
            int soundId = soundPool.load(this,rawIds[i],1);
            map.put(viewIds[i],soundId);
        }
    }

    private void play(int soundId){
        soundPool.play(soundId,1,1,0,0,1);
    }
    @Override
    public void onClick(View view) {
        play(map.get(view.getId()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(soundPool!=null)
            soundPool.release();
    }
}